#include <QtWidgets>
#include "window.h"



Window::Window(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Extract Select"));
    QPushButton* browseButton = new QPushButton(tr("&Browse..."), this);    //搜索按钮
    connect(browseButton, &QAbstractButton::clicked, this, &Window::browse);
    directoryComboBox = createComboBox(QDir::currentPath());

    resultLable = new QLabel;
    createFilesTable();

    QGridLayout* mainLayout = new QGridLayout(this);
    //寻找文件组件
    mainLayout->addWidget(new QLabel(tr("Directory")), 0, 0);
    mainLayout->addWidget(directoryComboBox, 0, 1);
    mainLayout->addWidget(browseButton, 0, 2);

    mainLayout->addWidget(filesTable, 1, 0, 1, 3);
    mainLayout->addWidget(resultLable, 3, 0, 1, 2);
}

Window::~Window()
{

}

void Window::browse()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open File"),
                                                    "/home", tr("Images(*.png *.jpg)"));
    if (!fileName.isEmpty())
    {
        if (directoryComboBox->findText(fileName) == -1)
            directoryComboBox->addItem(fileName);
        directoryComboBox->setCurrentIndex(directoryComboBox->findText(fileName));
        qDebug() << fileName << endl;
    }
}

void Window::find()
{

}

QComboBox* Window::createComboBox(const QString& text)
{
    QComboBox* comboBox = new QComboBox;
    comboBox->setEditable(true);
    comboBox->addItem(text);
    comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    return comboBox;
}

void Window::createFilesTable()
{
    filesTable = new QTableWidget(0, 2);
    filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);

    QStringList labels;
    labels << tr("Filename") << tr("Size");
    filesTable->setHorizontalHeaderLabels(labels);
    filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
    filesTable->verticalHeader()->hide();
    filesTable->setShowGrid(false);

    filesTable->setContextMenuPolicy(Qt::CustomContextMenu);
}
